﻿using System;
using System.Collections.Generic;
using UnityEngine;

namespace Sizebox.CharacterEditor
{
	// Token: 0x02000255 RID: 597
	[RequireComponent(typeof(CharacterEditor))]
	public class MaterialEdit : MonoBehaviour
	{
		// Token: 0x1700017D RID: 381
		// (get) Token: 0x06000CED RID: 3309 RVA: 0x000683CC File Offset: 0x000665CC
		// (set) Token: 0x06000CEE RID: 3310 RVA: 0x000683D4 File Offset: 0x000665D4
		public HashSet<MaterialWrapper> Materials { get; private set; }

		// Token: 0x06000CEF RID: 3311 RVA: 0x000683E0 File Offset: 0x000665E0
		private void Awake()
		{
			HashSet<Renderer> renderers = this.GetRenderers();
			this.Materials = new HashSet<MaterialWrapper>();
			foreach (Renderer renderer in renderers)
			{
				foreach (Material material in renderer.materials)
				{
					this.Materials.Add(new MaterialWrapper(material, this.count));
					this.count++;
				}
			}
		}

		// Token: 0x06000CF0 RID: 3312 RVA: 0x00068490 File Offset: 0x00066690
		private HashSet<Renderer> GetRenderers()
		{
			EntityBase component = base.GetComponent<EntityBase>();
			if (!component)
			{
				return null;
			}
			List<EntityBase> list = new List<EntityBase>();
			base.GetComponentsInChildren<EntityBase>(list);
			if (list.Contains(component))
			{
				list.Remove(component);
			}
			Renderer[] componentsInChildren = component.GetComponentsInChildren<Renderer>();
			HashSet<Renderer> hashSet = new HashSet<Renderer>(componentsInChildren);
			foreach (EntityBase entityBase in list)
			{
				Renderer[] componentsInChildren2 = entityBase.GetComponentsInChildren<Renderer>();
				foreach (Renderer item in componentsInChildren2)
				{
					if (hashSet.Contains(item))
					{
						hashSet.Remove(item);
					}
				}
			}
			return hashSet;
		}

		// Token: 0x06000CF1 RID: 3313 RVA: 0x0006856C File Offset: 0x0006676C
		public void AddMaterials(ICollection<Material> materials)
		{
			foreach (Material material in materials)
			{
				this.Materials.Add(new MaterialWrapper(material, this.count));
				this.count++;
			}
		}

		// Token: 0x06000CF2 RID: 3314 RVA: 0x000685E0 File Offset: 0x000667E0
		public void RemoveMaterials(HashSet<Material> matsToRemove)
		{
			List<MaterialWrapper> list = new List<MaterialWrapper>();
			foreach (MaterialWrapper materialWrapper in this.Materials)
			{
				if (matsToRemove.Contains(materialWrapper.Material))
				{
					list.Add(materialWrapper);
				}
			}
			foreach (MaterialWrapper item in list)
			{
				this.Materials.Remove(item);
			}
		}

		// Token: 0x06000CF3 RID: 3315 RVA: 0x000686A0 File Offset: 0x000668A0
		public void LoadPreset(MaterialDataSet materialData)
		{
			foreach (MaterialWrapper materialWrapper in this.Materials)
			{
				materialWrapper.ResetMaterial();
			}
			foreach (MaterialData materialData2 in materialData.matData)
			{
				foreach (MaterialWrapper materialWrapper2 in this.Materials)
				{
					if (materialWrapper2.Id.Equals(materialData2.id))
					{
						materialData2.LoadMaterialData(materialWrapper2);
						break;
					}
				}
			}
		}

		// Token: 0x04001203 RID: 4611
		private int count;
	}
}
